import { handleAuthGetUserInfo, handleGetUserPhomeNumber } from '../../utils/wx'
import { isLogin, toLogin, getUserInfo, clearCacheAll } from '../../utils/auth'
import http from '../../utils/http'
import { showModel, showNotify, showToast, showDialog } from '../../utils/index'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        userInfo: null,
        wallet: {
            amount: 0.00,
            freezeAmount: 0.00,
            totalAmount: 0.00
        },

        amount: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            beforeClose: (action) => new Promise((resolve) => {
                let { amount, wallet } = this.data
                if (action === 'confirm') {
                    if(amount > 0) {
                        if(amount > wallet.amount) {
                            resolve(false)
                        }
                    } else {
                        resolve(false)
                    }
                } else {
                    this.setData({ amount: null })
                    resolve(true)
                }
            })
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 用户登录信息
        this.setData({ userInfo: getUserInfo() });

        this.getMemberWallter();
    },

    goPage(e) {
        const { url } = e.currentTarget.dataset
        wx.navigateTo({ url });
    },

     /**
     * 获取我的钱包信息
     */
      async getMemberWallter() {
        let { userInfo } = this.data
        let res = await http.get('/api/mall/umsmember/getMyWallter', {memberId: userInfo.id})

        if(res.status) {
            this.setData({ wallet: res.data })
        }
    },

    showApplyWallet() {
        this.setData({ show: true })
    },
    beforClose(e) {
        console.log(e);
    },
    async onConfirm(e) {
        let { amount, wallet, userInfo } = this.data

        
        if(amount > 0) {
            if(amount > wallet.amount) {
                showToast({title: '余额不足'})
                return false
            } else {
                const { APPID } = getApp().globalData
                let res = await http.post('/api/mall/fmsmemberwithdraw/apply', {appId: APPID, memberId: userInfo.id, applyTakeAmount: amount})
                if(res.status) {
                    this.getMemberWallter()
                    this.setData({ amount: null, show: false })
                }
            }
        } else {
            showToast({title: '请输入正确金额'})
            return false
        }


        
    },
    onCancel() {
        this.setData({ showPwd: false, canSendSms: true })
    },
})